package cn.shop.cloud.redis;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @program: work
 * @ClassName MyRedisAutoConfiguration
 * @description:
 * @author: 李尧坤
 * @create: 2025−01-08 11:04
 * @Version 1.0
 **/

@Configuration
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnProperty(prefix = "redis",value = "host")
public class MyRedisAutoConfiguration {

    @Bean
    public  MyRedisTemplate myRedisTemplate(){
        return new MyRedisTemplate();
    }

    //装载连接池配置信息
    @Bean
    public JedisPool jedisPool(RedisProperties redisProperties){
        //添加信息
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(redisProperties.getPoolMaxTotal());
        config.setMaxIdle(redisProperties.getPoolMaxIdle());
        config.setMaxWaitMillis(redisProperties.getPoolMaxWait()*1000);//毫秒

        return  new JedisPool(config,
                redisProperties.getHost(),
                redisProperties.getPort(),
                redisProperties.getTimeout(),
                redisProperties.getPassword());

    }

}

